﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
   public class LoaichungchiDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Loaichungchi");
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Loaichungchi {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Loaichungchi)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Loaichungchi '{0}', N'{1}' ";
            sql = string.Format(sql, o.Maloai, o.Tenchungchi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Loaichungchi)obj;
            var sql = "set dateformat dmy; execute sp_Update_Loaichungchi '{0}', N'{1}' ";
            sql = string.Format(sql, o.Maloai, o.Tenchungchi);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_Loaichungchi where Tenchungchi like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Loaichungchi o)
        {
            string sql;
            string ten = o.Tenchungchi;

            if (o.Maloai != "")
                sql = @"Select * From tb_Loaichungchi Where Maloai like '%" + o.Maloai + "%' and Tenchungchi like N'%"+ten+"%' ";
            else
                return Search(ten);

            return ExecuteQuery(sql);
        }
        
        #endregion


        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(MaLoai) as Ma From tb_Loaichungchi";
            return ExecuteQuery(sql);
        }

        #endregion

    }
}
